import 'package:eyepetizer_example/model/base_change_notifier.dart';
import 'package:eyepetizer_example/model/common_item.dart';
import 'package:eyepetizer_example/utils/toast_util.dart';

import '../http/http_manager.dart';
import '../http/Url.dart';
import '../widget/loading_state_widget.dart';

class VideoDetailViewModel extends BaseChangeNotifier {
  List<ItemList> itemList = [];
  int _videoId = 0;

  void loadVideoData(int id) {
    _videoId = id;
    var videoUrl = '${Url.videoRelatedUrl}$id';
    HttpManager.requestData(videoUrl).then((res) {
      IssueList issue = IssueList.fromJson(res);
      itemList = issue.itemList??[];
      viewState = ViewState.done;
    }).catchError((e) {
      AppToast.showError(e.toString());
      viewState = ViewState.error;
    }).whenComplete(() => notifyListeners());
  }

  void retry() {
    viewState = ViewState.loading;
    notifyListeners();
    loadVideoData(_videoId);
  }

}